home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 108 / MacAddict108.iso / Software / Development / REALbasic 5.5.5.dmg / REALbasic 5.5.5 Mac OS X / Read Me / Office Automation / Office Automation Read Me.txt < prev    next >
Encoding:
Text File  |  2004-02-19  |  6.2 KB  |  194 lines

  1. Office Automation Overview
  2. --------------------------
  3.  
  4. There are 4 related Office classes built-in to REALbasic.
  5. Please note that these new classes will conflict with the old Office plugins,
  6. so make sure to remove the Office plugins from your plugins folder before
  7. launching REALbasic.
  8.  
  9. Office class
  10.     - This class contains all the enums you'd need for Office automation
  11.  
  12. ExcelApplication class
  13.     - This class is used to automate Excel
  14.  
  15. PowerPointApplication class
  16.     - This class is used to automate PowerPoint
  17.  
  18. WordApplication class
  19.     - This class is used to automate Word
  20.  
  21.  
  22. Office Automation Known Issues
  23. ------------------------------
  24.  
  25. Mac OS 9:
  26.     Not supported
  27.  
  28. OS X:
  29.     The OLE libraries are stored privately within Office, so you
  30.     must compile your application, and copy it to the Office folder
  31.     inside the Microsoft Office X parent folder.  
  32.     i.e. inside Mac OS X: Applications: Microsoft Office X: Office
  33.     For debugging, you need to copy the REALbasic IDE inside the Office folder.
  34.     We are working on getting apps to run outside the Office folder.
  35.  
  36.  
  37. Office Automation under REALbasic versus VisualBasic
  38. ----------------------------------------------------
  39.  
  40. Working from the Application class:
  41.  
  42.     There is an implied Application instance when you write VBA code from
  43.     within Excel, PowerPoint, or Word.  For Example:
  44.  
  45.         Dim pres as Presentation
  46.         Dim slide1 as Slide
  47.  
  48.         Set pres = Presentations.Add
  49.         ' The above is the same as saying:
  50.         ' Set pres = Application.Presentations.Add
  51.  
  52.         Set slide1 = pres.Slides.Add(1, ppLayoutText)
  53.  
  54.     If we're in PowerPoint this code would run just fine since it knows
  55.     what a Presentation object is.  Obviously if you typed this code in
  56.     either Word or Excel, it would generate errors.  So what does this
  57.     code look like in RB?  Here's the RB code:
  58.  
  59.         Dim PowerPoint as new PowerPointApplication
  60.         Dim pres as PowerPointPresentation
  61.         Dim slide1 as PowerPointSlide
  62.  
  63.         pres = PowerPoint.Presentations.Add
  64.         slide1 = pres.Slides.Add(1, Office.ppLayoutText)
  65.  
  66.     There's really only two differences here.  First off, all PowerPoint
  67.     objects are prefixed with "PowerPoint", all Word objects are prefixed
  68.     with "Word", and all Excel objects are prefixed with "Excel".
  69.     Another difference is that all constant values are in the Office module,
  70.     this is done so we don't clutter the namespace in RB.
  71.  
  72.  
  73. Passing parameters by name:
  74.  
  75.     RB doesn't support passing parameters by name, however you can still
  76.     achieve this with a bit of work.  First of all, you have to understand
  77.     how to use the OLEObject.  You can read up on the docs, but for brevity
  78.     here's an example that you can model after:
  79.  
  80.     Let's record a find and replace macro in Word and translate it to RB
  81.  
  82.         Selection.Find.ClearFormatting
  83.         Selection.Find.Replacement.ClearnFormatting
  84.         With Selection.Find
  85.             .Text = "find this"
  86.             .Replacement.Text = "replace with"
  87.             .Wrap = wdFindContinue
  88.             .Format = false
  89.             .MatchCase = false
  90.             .MatchWholeWord = false
  91.             .MatchWildcards = false
  92.             .MatchSoundsLike = false
  93.             .MatchAllWordForms = false
  94.         End With
  95.         Selection.Find.Execute Replace:=wdReplaceAll
  96.  
  97.     Ok, here's what it would look like in RB
  98.  
  99.  
  100.         Dim word as new WordApplication
  101.         Dim find as WordFind
  102.  
  103.         find = word.Selection.Find
  104.  
  105.         find.ClearFormatting
  106.         find.Replacement.ClearFormatting
  107.         find.text = "find this"
  108.         find.Replacement.Text = "replace with"
  109.         find.Wrap = Office.wdFindContinue
  110.         find.Format = false
  111.         find.MatchCase = false
  112.         find.MatchWholeWord = false
  113.         find.MatchWildcards = false
  114.         find.MatchSoundsLike = false
  115.         find.MatchAllWordForms = false
  116.  
  117.         // Now the fun stuff
  118.  
  119.         Dim replaceParam as new OLEParameter
  120.  
  121.         replaceParam.Value = Office.wdReplaceAll
  122.         // according to the docs on Find.Execute the Replace parameter is the 11th
  123.         replaceParam.Position = 11
  124.  
  125.         find.Execute replaceParam
  126.  
  127.     That's all there is too it.  Obviously the most painful bit is finding the
  128.     correct position of that named parameter.  That's about the only time when you
  129.     really need to launch VBA and look it up in their Object Browser.
  130.  
  131.  
  132. Conflicting keywords:
  133.  
  134.     There are certain reserved keywords in RB (these will usually be lighted in
  135.     a different color, such as 'Select' or 'End') that cannot be used as method
  136.     names or property names.  Unfortunately, Excel, as an example, utilitizes
  137.     some of these names in their methods/properties.  To get around this problem,
  138.     you can suffix the method/property name that you want access to with an
  139.     underscore character.  Here's an example:
  140.  
  141.         Excel.Range("A1", "A3").Select_
  142.  
  143.     Since the keyword 'Select' is reserved, you can suffix that with an
  144.     underscore character and RB will hand it off to Excel as "Select".
  145.  
  146.  
  147. Loading Office docs
  148. -------------------
  149.  
  150. If our autocomplete is not sufficient, or you want a higher level view of the
  151. Office classes, you can load up VBA and follow the Object Browser to see what
  152. each class supports.  If you're using the Windows IDE, you can view the Office
  153. Type Libraries, which are essentially the docs for Word, Excel, and PowerPoint.
  154. To load these up, go to File | Add ActiveX Components... and under the
  155. "References" tab, you can select the appropriate type library to view.
  156. For example, "Microsoft Word #.# Object Library" contains the docs for Word,
  157. where #.# is the version number.
  158.  
  159.  
  160. Trouble Shooting
  161. ----------------
  162.  
  163. Q: How do I know if I have the OLE libraries installed?
  164. A: One easy way is to load up Visual Basic Editor (under the Tools
  165.    and Macros menu), and do some automation with VBA.
  166.    Here are the steps you can use to automate PowerPoint from Word:
  167.    
  168.    1. Load up Word
  169.    2. Load up Visual Basic Editor in Word
  170.    3. Insert a UserForm
  171.    4. Add a CommandButton to the form
  172.    5. In the click event of the button, put in this code:
  173.       Dim obj as Object
  174.       Set obj = CreateObject("PowerPoint.Application")
  175.       obj.Activate
  176.    6. Run the program and click on the button
  177.    7. If PowerPoint loads up and you don't get any errors, then the OLE
  178.       libraries are installed.
  179.  
  180. Q: What are the possible errors I can catch?
  181. A: Errors come through OLE, so you need to catch OLEExceptions.
  182.    This will report the last command that failed along with any
  183.    additional information about the exception.
  184.  
  185.     Dim word as new WordApplication
  186.  
  187.     word.ShowClipboard
  188.     
  189.     exception err as OLEException
  190.         msgbox err.message
  191.  
  192.  
  193. Last Updated: February 19, 2004
  194.